package com.hyperplan.neptune.parser;

import java.util.ArrayList;
import java.util.List;

import com.hyperplan.neptune.lexer.Token;

public abstract class SequenceParser implements Parser<Object[]> {

	private List<Parser<Object>> parsers;
	
	public SequenceParser() {
		parsers = new ArrayList<Parser<Object>>();
	}
	
	public void add(Parser<? extends Object> parser) {
		parsers.add(new ConverterParser<Object>(parser));
	}
	
	public ParserSolution<Object[]> parse(List<Token> tokens, int index)
			throws ParserException {
		Object[] result = new Object[parsers.size()];
		for (int i = 0; i < parsers.size(); i++) {
			ParserSolution<Object> solution = parsers.get(i).parse(tokens, index);
			result[i] = solution.getElement();
			index = solution.getIndex();
		}
		return new ParserSolution<Object[]>(result, index);
	}
}
